<template>
  <div style="width: 100%;padding-top: 20px;">
    <!---->
    <el-form ref="userInfo3" :rules="chgphoneFules" :model="userInfo3" status-icon label-position="left"
             label-width="150px" style="width: 50%; ">
      <el-form-item label="原手机号" prop="oldMobile">
        <el-input v-model="userInfo3.oldMobile" placeholder="请输入原手机号"/>
      </el-form-item>
      <el-form-item label="新手机号" prop="newMobile">
        <el-input v-model="userInfo3.newMobile" placeholder="请输入新手机号"/>
      </el-form-item>
      <el-form-item label="新手机号短信验证码" prop="checkSms">
        <el-input v-model="userInfo3.checkSms" placeholder="请输入新手机短信验证码" style="width:64%;"/>
        <el-button :loading="phoneloading" type="primary" :disabled="phoneDisabled" style="width: 100px;"
                   @click.stop="checkSmsesend">{{ phoneText }}
        </el-button>
      </el-form-item>
      <el-form-item label="登录密码" prop="password">
        <el-input v-model="userInfo3.password" type="password" auto-complete="new-password" placeholder="请输入登录密码"/>
      </el-form-item>
      <el-form-item style="margin-top:60px;">
        <el-button type="primary" @click="chgphone">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {doGet, doPost} from '@/api'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'realcheck',
  filters: { },
  components: {
    Pagination
  },
  data() {
    const validateCheckSms = (rule, value, callback) => {
      if (value.length != 6) {
        callback(new Error('短信验证码必须为6位'))
      } else {
        callback()
      }
    }
    const mobileValidate = (rule, value, callback) => {
      let reg = /^[1][0-9]{10}$/;
      if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度应大于6'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      unionnos: [],
      total1: 0,
      userInfo3: {
        oldMobile: '',
        newMobile: '',
        checkSms: '',
        password: ''
      },
      chgphoneFules: {
        oldMobile: [
          { required: true, message: '原手机号不能为空', trigger: 'blur' },
          { validator: mobileValidate, trigger: 'blur' }
        ],
        newMobile: [
          { required: true, message: '新手机号不能为空', trigger: 'blur' },
          { validator: mobileValidate, trigger: 'blur' }
        ],
        checkSms: [
          { required: true, message: '新手机号短信验证码不能为空', trigger: 'blur' },
          { validator: validateCheckSms, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '登录密码不能为空', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      phoneText: '获取验证码',
      phoneloading: false,
      phoneDisabled: true,
    }
  },
  created() {
  },
  watch: {
    userInfo3: {
      handler: function(val) {
        let reg = /^[1][0-9]{10}$/;
        if (!reg.test(val.newMobile)) {
          this.phoneDisabled = true
        } else {
          if (!this.timer) {
            this.phoneDisabled = false
          }
        }
      },
      deep: true
    }
  },
  methods: {
    checkSmsesend() {
      this.phoneloading = true
      doGet('sendsms.html',{ phone: this.userInfo3.newMobile }).then((response) => {
        this.phoneDisabled = true
        this.phoneloading = false
        this.setTime(300)
      }).catch((error) => {
        this.$notify.error({
          title: '失败',
          message: error.message
        })
        this.phoneloading = false
      })
    },

    setTime(time) {
      if (time <= 0) {
        this.phoneText = '重新获取'
        this.phoneDisabled = false
        clearTimeout(this.timer)
        return
      }
      this.phoneText = `还剩${time}s`
      this.timer = setTimeout(() => {
        time--
        this.setTime(time)
      }, 1000)
    },
    chgphone() {
      this.$refs.userInfo3.validate((valid) => {
        if (valid && !this.loading) {
          this.loading = true
          doGet('userinfo.html?op=chgphone', this.userInfo3) .then(response => {
            this.loading = false
            this.$router.back()
            this.$notify.success({
              title: '成功',
              message: '手机号修改成功'
            })
          }).catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
            this.loading = false
          })
        }
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
